package com.zjuee.web.tporder.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zjuee.base.bean.response.CommonResultCode;
import com.zjuee.base.constant.DictionaryConstants;
import com.zjuee.base.dto.common.login.LoginDto;
import com.zjuee.base.exception.BaseException;
import com.zjuee.base.utils.DesensitizationUtil;
import com.zjuee.core.translation.service.TranslateData;
import com.zjuee.service.TransOrderReplyService;
import com.zjuee.base.dto.common.tporder.TransOrderReplyDto;
import com.zjuee.base.model.common.tporder.WkTransOrderCompanyEntity;
import com.zjuee.base.model.common.tporder.WkTransOrderMaterialEntity;
import com.zjuee.base.model.common.tporder.WkTransOrderReplyEntity;
import com.zjuee.base.model.common.tporder.WkTransStatisticsEntity;
import com.zjuee.vo.WkTransOrderCompanyVo;
import com.zjuee.vo.WkTransOrderMaterialVo;
import com.zjuee.vo.WkTransOrderReplyVo;
import com.zjuee.web.tporder.mapper.TransOrderCompanyMapper;
import com.zjuee.web.tporder.mapper.TransOrderMaterialMapper;
import com.zjuee.web.tporder.mapper.TransOrderReplyMapper;
import com.zjuee.web.tporder.mapper.TransStatisticsMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

@Slf4j
@Service
public class TransOrderReplyImpl implements TransOrderReplyService {
    @Resource
    private TransStatisticsMapper transStatisticsMapper;
    @Resource
    private TransOrderReplyMapper transOrderReplyMapper;
    @Resource
    private TransOrderCompanyMapper transOrderCompanyMapper;
    @Resource
    private TransOrderMaterialMapper transOrderMaterialMapper;
    @Resource
    private TranslateData translateData;
    @Resource
    private LoginDto loginDto;

    @Override
    public Long add(WkTransOrderReplyVo type) {
        return null;
    }

    @Override
    public boolean delete(Long id, Long validateId) {
        return false;
    }

    @Override
    public boolean update(WkTransOrderReplyVo type) {
        UpdateWrapper<WkTransOrderReplyEntity> updateWrapper=new UpdateWrapper<>();
        WkTransOrderReplyEntity wkTransOrderReplyEntity = new WkTransOrderReplyEntity();
        //处置企业
        //是否接收 (0未接收：1：确认接收 2：拒收)
        wkTransOrderReplyEntity.setWtorSfjs(type.getWtorSfjs());
        //接收日期
        wkTransOrderReplyEntity.setWtorWfjsdwjsrq(new Date());
        updateWrapper.eq("id",type.getId());
        int affectRows = transOrderReplyMapper.update(wkTransOrderReplyEntity, updateWrapper);

        //总览表添加转出联单数量和废物量
        if (type.getWtorSfjs() == 1) {
            QueryWrapper<WkTransStatisticsEntity> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("wts_district_code", 3);
            queryWrapper.eq("wts_parent_code", 3333);
            queryWrapper.eq("wts_type", 3);
            WkTransStatisticsEntity wkTransStatisticsEntity = transStatisticsMapper.selectOne(queryWrapper);
            UpdateWrapper<WkTransStatisticsEntity> updateWrapper1 = new UpdateWrapper<>();
            wkTransStatisticsEntity.setWtsTransferInNum(wkTransStatisticsEntity.getWtsTransferInNum().add(new BigDecimal("1")));
            //转移联单编号wtor_ksldbh
            //根据联单编号查询联单的废物信息
            QueryWrapper<WkTransOrderMaterialEntity> queryWrapper1 = new QueryWrapper<>();
            queryWrapper1.eq("wtoa_id", type.getWtorKsldbh());
            WkTransOrderMaterialEntity wkTransOrderMaterialEntity = transOrderMaterialMapper.selectOne(queryWrapper1);
            wkTransStatisticsEntity.setWtsTransferIn(wkTransStatisticsEntity.getWtsTransferIn().add(wkTransOrderMaterialEntity.getWtomZysl()));
            updateWrapper1.eq("id", wkTransStatisticsEntity.getId());
            transStatisticsMapper.update(wkTransStatisticsEntity, updateWrapper1);
        }

        return affectRows > 0;
    }

    @Override
    public WkTransOrderReplyVo selectById(Long id, Long validateId) {
        return null;
    }

    @Override
    public Page<WkTransOrderReplyVo> list(TransOrderReplyDto dto) {
        Long orgId = loginDto.getOrgBusinessId();
        if (orgId == null) {
            throw new BaseException(CommonResultCode.AUTH_EXP);
        }
        dto.setBciId(orgId);
        Page<WkTransOrderReplyVo> page = transOrderReplyMapper.getTransOrderReplyList(dto.getPage().convert(), dto);

        List<Long> list = page.getRecords().stream().map(WkTransOrderReplyVo::getId).collect(Collectors.toList());
        if (list.isEmpty()){
            return page;
        }
        QueryWrapper<WkTransOrderMaterialEntity> wrapper = new QueryWrapper<>();
        wrapper.in("wtoa_id",list).eq("WTOM_TYPE", DictionaryConstants.TRANS_ORDER_IN);
        List<WkTransOrderMaterialEntity> materialEntityList = transOrderMaterialMapper.selectList(wrapper);
        QueryWrapper<WkTransOrderCompanyEntity> queryWrapper = new QueryWrapper<>();
        queryWrapper.in("wtoa_id",list).eq("WTOC_TYPE", DictionaryConstants.TRANS_ORDER_IN);
//        List<WkTransOrderCompanyEntity> companyEntityList = transOrderCompanyMapper.selectList(queryWrapper);
        for (WkTransOrderReplyVo replyVo : page.getRecords()) {
            List<WkTransOrderMaterialEntity> materialEntities = materialEntityList.stream().filter(m -> m.getWtoaId().equals(replyVo.getId())).collect(Collectors.toList());
//            List<WkTransOrderCompanyEntity> companyEntities = companyEntityList.stream().filter(c -> c.getWtoaId().equals(replyVo.getId())).collect(Collectors.toList());
            replyVo.setWtoaReceive(materialEntities.get(0).getWtomJssl());
            replyVo.setWtoaCarry(materialEntities.get(0).getWtomZysl());
            replyVo.setWtoaScode(materialEntities.get(0).getWtomFwdm());
            replyVo.setWtoaName(materialEntities.get(0).getWtomFwmc());
            replyVo.setWtorWfycrqStr(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(replyVo.getWtorWfycrq()));
            if (replyVo.getWtorWfjsdwjsrq() != null){
                replyVo.setWtorWfjsdwjsrqStr(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(replyVo.getWtorWfjsdwjsrq()));
            }
            replyVo.setWtorWfycdwlxr(DesensitizationUtil.desensitizedName(replyVo.getWtorWfycdwlxr()));
            replyVo.setWtorFwycdwlxrsj(DesensitizationUtil.desensitizedPhoneNumber(replyVo.getWtorFwycdwlxrsj()));
            replyVo.setWtorWfjsdwlxr(DesensitizationUtil.desensitizedName(replyVo.getWtorWfjsdwlxr()));
            replyVo.setWtorWfjsdwlxrsj(DesensitizationUtil.desensitizedPhoneNumber(replyVo.getWtorWfjsdwlxrsj()));
        }
        return page;
    }

    @Override
    public LambdaQueryWrapper<WkTransOrderReplyVo> fuzzyQuery(TransOrderReplyDto dto) {
        return null;
    }

    @Override
    public WkTransOrderReplyVo getTransOrderReply(Long id) {
        WkTransOrderReplyEntity wkTransOrderReplyEntity = transOrderReplyMapper.selectById(id);

        QueryWrapper<WkTransOrderCompanyEntity> queryWrapper1 = new QueryWrapper<>();
        queryWrapper1.eq("wtoa_id", id);
        List<WkTransOrderCompanyEntity> wkTransOrderCompanyEntitys = transOrderCompanyMapper.selectList(queryWrapper1);

        QueryWrapper<WkTransOrderMaterialEntity> queryWrapper2 = new QueryWrapper<>();
        queryWrapper2.eq("wtoa_id", id);
        List<WkTransOrderMaterialEntity> wkTransOrderMaterialEntitys = transOrderMaterialMapper.selectList(queryWrapper2);


        WkTransOrderReplyVo wkTransOrderReplyVo =  new WkTransOrderReplyVo();

        List<WkTransOrderCompanyVo> wkTransOrderCompanyVos = new ArrayList();

        List<WkTransOrderMaterialVo> wkTransOrderMaterialVos = new ArrayList();

        BeanUtils.copyProperties(wkTransOrderReplyEntity, wkTransOrderReplyVo);
        for(WkTransOrderCompanyEntity wkTransOrderCompanyEntity : wkTransOrderCompanyEntitys){
            WkTransOrderCompanyVo wkTransOrderCompanyVo = new WkTransOrderCompanyVo();
            BeanUtils.copyProperties(wkTransOrderCompanyEntity, wkTransOrderCompanyVo);
            wkTransOrderCompanyVos.add(wkTransOrderCompanyVo);
        }
        for(WkTransOrderMaterialEntity wkTransOrderMaterialEntity : wkTransOrderMaterialEntitys){
            WkTransOrderMaterialVo wkTransOrderMaterialVo = new WkTransOrderMaterialVo();
            BeanUtils.copyProperties(wkTransOrderMaterialEntity, wkTransOrderMaterialVo);
            translateData.translateData(wkTransOrderMaterialVo,"transOrderHandle",
                    DictionaryConstants.PB_WASTE_SHAPE,
                    DictionaryConstants.PB_WASTE_PACK_TYPE
//                    RedisCacheType.DISPOSE_STYPE.toString()
            );
            wkTransOrderMaterialVos.add(wkTransOrderMaterialVo);
        }
        wkTransOrderReplyVo.setWtorWfycrqStr(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(wkTransOrderReplyVo.getWtorWfycrq()));
        if (wkTransOrderReplyVo.getWtorWfjsdwjsrq() != null){
            wkTransOrderReplyVo.setWtorWfjsdwjsrqStr(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(wkTransOrderReplyVo.getWtorWfjsdwjsrq()));
        }

        wkTransOrderReplyVo.setWkTransOrderCompanyVos(wkTransOrderCompanyVos);
        wkTransOrderReplyVo.setWkTransOrderMaterialVos(wkTransOrderMaterialVos);
        wkTransOrderReplyVo.setWtorWfycdwlxr(DesensitizationUtil.desensitizedName(wkTransOrderReplyVo.getWtorWfycdwlxr()));
        wkTransOrderReplyVo.setWtorFwycdwlxrsj(DesensitizationUtil.desensitizedPhoneNumber(wkTransOrderReplyVo.getWtorFwycdwlxrsj()));
        wkTransOrderReplyVo.setWtorWfjsdwlxr(DesensitizationUtil.desensitizedName(wkTransOrderReplyVo.getWtorWfjsdwlxr()));
        wkTransOrderReplyVo.setWtorWfjsdwlxrsj(DesensitizationUtil.desensitizedPhoneNumber(wkTransOrderReplyVo.getWtorWfjsdwlxrsj()));
        return wkTransOrderReplyVo;
    }
}
